package com.sdleyou.treepro.business.api.tree;

import com.sdleyou.treepro.common.model.dto.ExchangeDTO;
import com.sdleyou.treepro.common.model.utils.ResultVOUtil;
import com.sdleyou.treepro.common.model.vo.ResultVO;
import com.sdleyou.treepro.common.service.TreeManager;
import com.sdleyou.treepro.common.service.UserTreeManager;
import com.sdleyou.treepro.system.version.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("/api")
public class UserTreeController {

    @Autowired
    private UserTreeManager userTreeManager;
    @Autowired
    private TreeManager treeManager;

    /**
     * 用户选择游戏内养殖的树苗
     *
     * @param userId
     * @param treeId
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/tree")
    public ResultVO initGameTree(@RequestParam("user_id") @NotEmpty(message = "用户ID不能为空") String userId,
                                 @RequestParam("tree_id") @NotEmpty(message = "树木ID不能为空") String treeId) {
        userTreeManager.initGameTree(userId, treeId);
        return ResultVOUtil.success();
    }

    /**
     * 用户通过能量兑换树苗
     *
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/tree/exchange")
    public ResultVO exchangeTree(@RequestBody @Valid ExchangeDTO exchangeDTO) {
        userTreeManager.exchangeTree(exchangeDTO);
        return ResultVOUtil.success();
    }

    /**
     * 获取树信息
     */
    @ApiVersion(1)
    @PostMapping("/{version}/tree/info")
    public ResultVO exchangeTree(@RequestParam("user_id") @NotEmpty(message = "用户ID不能为空") String userId,
                                 @RequestParam("tree_id") @NotEmpty(message = "树木ID不能为空") String treeId) {
        return ResultVOUtil.success(userTreeManager.getTreeInfoById(userId, treeId));
    }

    /**
     * 获取树养殖信息
     */
    @ApiVersion(1)
    @PostMapping("/{version}/tree/plant/info")
    public ResultVO getTreePlantInfo(@RequestParam("user_id") @NotEmpty(message = "用户ID不能为空") String userId,
                                     @RequestParam("tree_id") @NotEmpty(message = "树木ID不能为空") String treeId) {
        return ResultVOUtil.success(userTreeManager.getTreePlantInfo(userId, treeId));
    }

    /**
     * 所有树木列表
     */
    @ApiVersion(1)
    @PostMapping("/{version}/tree/list")
    public ResultVO treeList(@RequestParam("user_id") @NotEmpty(message = "用户ID不能为空") String userId) {
        return ResultVOUtil.success(treeManager.treeList());
    }

    /**
     * 减少或保留虫子减少的成长值
     */
    @ApiVersion(1)
    @PostMapping("/{version}/tree/pest/status")
    public ResultVO changePestStatus(@RequestParam("user_id") @NotEmpty(message = "用户ID不能为空") String userId,
                                     @RequestParam("tree_id") @NotEmpty(message = "树木ID不能为空") String treeId,
                                     @RequestParam("type") @NotEmpty(message = "类型不能为空") String type) {
        userTreeManager.changePestStatus(userId, treeId, type);
        return ResultVOUtil.success();
    }
}
